package com.zziq.cloudpicturebank.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zziq.cloudpicturebank.annotation.AuthCheck;
import com.zziq.cloudpicturebank.common.BaseResponse;
import com.zziq.cloudpicturebank.common.ResultUtils;
import com.zziq.cloudpicturebank.constant.UserConstant;
import com.zziq.cloudpicturebank.model.dto.space.SpaceQueryRequest;
import com.zziq.cloudpicturebank.model.dto.vipcode.VipCodeBatchAddRequest;
import com.zziq.cloudpicturebank.model.dto.vipcode.VipCodeQueryRequest;
import com.zziq.cloudpicturebank.model.entity.User;
import com.zziq.cloudpicturebank.model.entity.VipCode;
import com.zziq.cloudpicturebank.service.UserService;
import com.zziq.cloudpicturebank.service.VipCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName VipCodeController
 * @Description
 * @Created by ZZIQ
 **/
@Slf4j
@RestController
@RequestMapping("/vipCode")
public class VipCodeController {
    @Resource
    private VipCodeService vipCodeService;

    @Resource
    private UserService userService;

    /**
     * 批量增加会员兑换码
     * @param vipCodeBatchAddRequest
     * @param request
     * @return
     */
    @PostMapping("/batchAdd/vipCode")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> batchAddVipCode(@RequestBody VipCodeBatchAddRequest vipCodeBatchAddRequest,
                                                 HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        vipCodeService.batchAddVipCode(vipCodeBatchAddRequest,loginUser);
        return ResultUtils.success(true);
    }

    /**
     *        管理页
     * 分页获取会员兑换码列表（仅管理员可用）
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<VipCode>> listVipCodeByPage(@RequestBody VipCodeQueryRequest vipCodeQueryRequest) {
        long current = vipCodeQueryRequest.getCurrent();
        long size = vipCodeQueryRequest.getPageSize();
        // 查询数据库
        Page<VipCode> vipCodePage = vipCodeService.page(new Page<>(current, size),
                vipCodeService.getQueryWrapper(vipCodeQueryRequest));
        return ResultUtils.success(vipCodePage);
    }
}




